const { defineConfig } = require("@vue/cli-service");
const webpack = require("webpack");
const { resolve } = require('path');
const { ModuleFederationPlugin } = require("@module-federation/enhanced");

process.env.VUE_APP_VERSION = require('./package.json').version;

const env = {
  NODE_ENV: process.env.NODE_ENV
};

for (const key in process.env) {
  if (key.startsWith('VUE_APP_')) {
    // 将环境变量中的 VUE_APP_ 前缀去掉
    env[key.slice(8)] = process.env[key];
  }
}

module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  devServer: {
    port: 3666,
    host: '0.0.0.0',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
      "Access-Control-Allow-Headers":
        "X-Requested-With, content-type, Authorization",
    }
  },
  publicPath: process.env.NODE_ENV == 'development' ? 'http://localhost:3666/' : process.env.VUE_APP_PUBLIC_PATH,
  configureWebpack: {
    devtool: false,
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        'wxy_core': 'wxy_core'
      }
    },
    experiments: {
      topLevelAwait: true
    },
    plugins: [
      new ModuleFederationPlugin({
        name: 'wxy_logic',
        filename: 'wxy_logic.js',
        exposes: {
          // './components/web': './src/components/web/index.js',
          '.': './src/expose.js'
        },
        shared: {
          vue: { singleton: true, requiredVersion: '^3.5.0', eager: true },
          qs: { singleton: true, requiredVersion: '^6.14.0', eager: true },
        },
        remotes: {
          wxy_core: `wxy_core@${process.env.VUE_APP_CORE_FEDERATION_HOST}wxy_core.js`
        },
        dev: true
      })
    ]
  },
  chainWebpack: (config) => {
    config.optimization.delete('splitChunks');
  }
});
